package com.johndriver.selenium.utils;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 获取不同浏览器
 * 日期 2021/5/22 14:17
 * 创建者：john driver
 */
public class DriverUtil {
   /**
    * 功能描述：获取Chrome浏览器
   */
    public static WebDriver getChromeDriver(String driverPath){
        WebDriver driver=null;
        //linux
        ChromeOptions chromeOptions = new ChromeOptions();
        DesiredCapabilities chromeCap = DesiredCapabilities.chrome();
        if(OsUtil.isWindows()){
            // window开发，需要界面
            chromeOptions.addArguments("--no-sandbox","--disable-gpu","--window-size=1290,1080");
        }else {
            // linux 部署
            chromeOptions.addArguments("--headless","--no-sandbox","--disable-gpu","--window-size=1290,1080");
            chromeCap.setCapability("chromeOptions", chromeOptions);
        }
        try {
            if(OsUtil.isWindows()){
                System.setProperty("webdriver.chrome.driver", driverPath);
                driver = new ChromeDriver(chromeOptions);
            }else { //linux
                URL remoteAddress = new URL("http://localhost:9515");
                driver = new RemoteWebDriver(remoteAddress, chromeCap);
            }
        }catch (MalformedURLException e){
            e.printStackTrace();
        }

        return driver;
    }
    /**
     * 功能描述：获取有界面FirefoxDriver
    */
    public static WebDriver getFirefoxDriver(){
        return getFirefoxDriver(false);
    }
    /**
     * 功能描述：获取FirefoxDriver
    */
    public static WebDriver getFirefoxDriver(boolean headless){
        FirefoxProfile firefoxProfile = new FirefoxProfile(new File("C:\\Users\\DQC\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\tdwoqqoz.default"));
        //跳过安全验证ssl
        firefoxProfile.setAcceptUntrustedCertificates(true);
        firefoxProfile.setAssumeUntrustedCertificateIssuer(true);

        FirefoxOptions firefoxOptions = new FirefoxOptions();
        firefoxOptions.setProfile(firefoxProfile);
        firefoxOptions.setAcceptInsecureCerts(true);
        firefoxOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
        //无界面
        if(headless){
            firefoxOptions.setHeadless(true);
        }
        System.setProperty("webdriver.gecko.driver","src/main/resources/geckodriver.exe");
        return new FirefoxDriver(firefoxOptions);
    }
}
